package com.qingyun.form

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView
import com.qingyun.form.base.FormGroup
import com.qingyun.form.base.FormGroupAdapter
import com.qingyun.form.base.FormItemDecoration
import com.qingyun.form.base.FormLayoutManager
import com.qingyun.form.entity.FormSelectItem
import com.qingyun.form.internal.render.FormRender
import com.qingyun.form.provider.SimpleDataProvider

class MainActivity : AppCompatActivity() {
    private val adapter = FormGroupAdapter()
    private val formGroup: FormGroup
        get() = adapter.formGroup
    private lateinit var render: FormRender

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        findViewById<Button>(R.id.btn_validate).setOnClickListener {
            val result = formGroup.validateForm()
            if (result.successfully()) {
                Toast.makeText(this, "校验成功", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val text = "表单校验失败：${result.message}"
            Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
        }
        findViewById<Button>(R.id.btn_viewonly).setOnClickListener {
            formGroup.viewonly = !formGroup.viewonly
            it as Button
            it.text = if (formGroup.viewonly) "可编辑" else "只读"
        }

        findViewById<RecyclerView>(R.id.recyclerView).apply {
            adapter = this@MainActivity.adapter
            layoutManager = FormLayoutManager(this@MainActivity, formGroup)
            addItemDecoration(FormItemDecoration(this@MainActivity).apply {
                height = 2
                paddingBottom = 200
            })
        }
        findViewById<Button>(R.id.btn_entity).setOnClickListener {
            val text = "实体值：${render.toJson()}"
            Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
            Log.d("rae", text)
        }
        render = FormRender(this, adapter, supportFragmentManager)
        editTextDemo()
        selectDemo()
        render.render()
    }

    private fun selectDemo() {
        val data = mutableListOf<FormSelectItem>().apply {
            for (i in 0 until 20) {
                val item = FormSelectItem("演示项$i", "parent$i", order = -1)
                this.add(item)
                if (i < 2) {
                    // 添加子项
                    for (k in 0 until 5) {
                        item.addChild(FormSelectItem("演示项${i}-子项$k", "node$i-$k", order = k))
                    }
                }
            }

            val item = FormSelectItem("多级演示项", "parent").apply {
                addChild(FormSelectItem("二级选项1", "level2-1"))
                addChild(FormSelectItem("二级选项2", "level2-2"))
                addChild(FormSelectItem("二级选项3", "level2-3").apply {
                    addChild(FormSelectItem("三级选项1", "level3-1"))
                    addChild(
                        FormSelectItem(
                            "三级选项222222222222333333333333333444444444",
                            "level3-2"
                        ).apply {
                            addChild(FormSelectItem("四级选项1", "level4-1"))
                            addChild(FormSelectItem("四级选项2", "level4-2"))
                            addChild(FormSelectItem("四级选项3", "level4-3"))
                            addChild(FormSelectItem("四级选项3", "level4-4"))
                        })
                    addChild(FormSelectItem("三级选项3", "level3-3"))
                    addChild(FormSelectItem("三级选项4", "level3-4"))
                })
            }
            this.add(item)
        }
        render.addGroupTitle("选择框示例")
        render.addSelect(
            SimpleDataProvider(data),
            "简单的选择框",
            "selectFirstValue",
            null,
            dialogTitle = "我是自定义标题"
        )
        render.addSelect(
            SimpleDataProvider(data).apply {
                pathSeparator = "-"
                enableCheckParent = true
            },
            "自动选择三级",
            "selectSecondValue",
            "level3-1"
        )
    }


    private fun editTextDemo() {
        val etName = "ET"
        render.addGroupTitle("EditText示例：")
        render.addEditText("姓", "firstName", "",true)
        render.addEditText("名", "lastName", "只读Value").apply {
            viewOnly = true
        }
        render.addDate("普通日期", "firstDate")
        render.addDate("初始化日期", "secondDate", "2025-06-30")
        render.addDate("指定范围日期（20250620~20250710）","thirdDate").apply {
            startDate = "2025-06-20"
            endDate = "2025-07-10"
        }
        render.addRadioGroup("单选1", "isRadioFirst")
        render.addRadioGroup("单选2", "isRadioSecond").apply {
            hint = "选一个吧"
        }
        render.addCheckBox("CheckBox示例", "isCheckBoxFirst", false.toString(), true).apply {
            hint = "打开开关"
            setCheckBoxLabel("开", "关")
        }
    }
}